<%@page import="com.bjgov.scoresys.dao.ScoreHistoryDAO"%>
<%@include file="/pub/import.jsp"%>
<%@include file="/pub/header.jsp"%>
<%
    int projectId = RequestUtil.getIntParameter(request,"projectId", 0);
    int treeId = RequestUtil.getIntParameter(request,"treeId", 0);
    Project project = ProjectHandler.getProject(projectId);
    int[] pidarr = new int[]{0,0,0,0};
    float buildUnitScore = 0.0f;
    float constructUnitScore = 0.0f;
    float designUnitScore = 0.0f;
    float supervisorUnitScore = 0.0f;
    
    if(!StringUtils.isEmpty(project.getBuildUnit())) {
      pidarr[0] = 1;
      List<ScoreHistory> historyList = ScoreHistoryHandler.getByUserIdProjectIdTopCateId(user.getId(), projectId, 1);
      for(ScoreHistory history: historyList) {
        buildUnitScore += (history.getStandardScore()-history.getDecScore());
      }
    }
    if(!StringUtils.isEmpty(project.getConstructUnit())) {
      pidarr[1] = 3;
      List<ScoreHistory> historyList = ScoreHistoryHandler.getByUserIdProjectIdTopCateId(user.getId(),projectId, 3);
      for(ScoreHistory history: historyList) {
        constructUnitScore += (history.getStandardScore()-history.getDecScore());
      }
    }
    if(!StringUtils.isEmpty(project.getSupervisorUnit())) {
      pidarr[2] = 40;
      List<ScoreHistory> historyList = ScoreHistoryHandler.getByUserIdProjectIdTopCateId(user.getId(), projectId, 40);
      for(ScoreHistory history: historyList) {
        supervisorUnitScore += (history.getStandardScore()-history.getDecScore());
      }
    }    
    if(!StringUtils.isEmpty(project.getDesignUnit())) {
      pidarr[3] = 44;
      List<ScoreHistory> historyList = ScoreHistoryHandler.getByUserIdProjectIdTopCateId(user.getId(),projectId, 44);
      for(ScoreHistory history: historyList) {
        designUnitScore += (history.getStandardScore()-history.getDecScore());
      }
    }
    List<ScoreTree> treeList = ScoreTreeHandler.loadByPIDs(pidarr);
    List<ProjectItems> projectItemsList = ProjectItemsHandler.getByProjectIdTreeId(projectId, treeId);
    Map<Integer,ScoreHistory>  hasScoreItemMap = new HashMap<Integer,ScoreHistory>();
    if(projectItemsList!=null && projectItemsList.size()>0) {
      String projectItemIds = "";
      for(int i=0; i<projectItemsList.size(); i++) {
        projectItemIds += projectItemsList.get(i).getScoreItem().getId();
        if(i!=projectItemsList.size()-1) projectItemIds += ","; 
      }
      List<ScoreHistory> hasScoreItemList = ScoreHistoryHandler.getByUserIdProjectIdItemIds(user.getId(),projectId,projectItemIds);
      for(ScoreHistory item: hasScoreItemList) {
        hasScoreItemMap.put(item.getProjectItemId(),item);
      }
    }

    List<ProjectItems> allProjectItems = ProjectItemsHandler.getByProject(projectId);
    Map<Integer, String> allProjectItemMap = new HashMap<Integer,String>();
    Map<Integer, String> allProjectItemTreeMap = new HashMap<Integer,String>();
    for(ProjectItems p: allProjectItems) {
      allProjectItemMap.put(p.getScoreItem().getId(), "true");
      allProjectItemTreeMap.put(p.getScoreItem().getScoreTree().getId(), "true");
    }
    context.put("allProjectItemMap", allProjectItemMap);    
    context.put("allProjectItemTreeMap", allProjectItemTreeMap);      
    
    UserProject up = UserProjectHandler.getUserProjectByUidPid(user.getId(),projectId);
    context.put("userProject", up);
    context.put("project",project);
    context.put("treeList",treeList);
    context.put("buildUnitScore", buildUnitScore);    
    context.put("constructUnitScore", constructUnitScore);
    context.put("designUnitScore", designUnitScore);
    context.put("supervisorUnitScore", supervisorUnitScore);
    context.put("totalScore", buildUnitScore+constructUnitScore+designUnitScore+supervisorUnitScore);
    context.put("hasScoreItemMap", hasScoreItemMap);
    context.put("itemList", projectItemsList);
    context.put("tree", ScoreTreeHandler.loadById(treeId));    
    context.put("DateUtil", new DateUtil());
    
    out.println(TemplateUtil.getOutput(context,"/view/judge/scoreProjectByPersonFinish.html"));
%>
<%@include file="/pub/footer.jsp"%>